package com.zys.hotel_management.controller;


import com.zys.hotel_management.common.Response;
import com.zys.hotel_management.entity.Booking;
import com.zys.hotel_management.service.BookingService;
import com.zys.hotel_management.utils.UserContext;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/bookings")
public class BookingController {
    @Resource
    private BookingService bookingService;

    @PostMapping
    public Response<Void> createBooking(@RequestBody Booking booking) {
        try {
            bookingService.createBooking(booking);
            return Response.success(null);
        } catch (Exception e) {
            return Response.fail(500, "创建预订失败: " + e.getMessage());
        }
    }

    @GetMapping
    public Response<List<Booking>> getAllBookings() {
        List<Booking> bookings = bookingService.getAllBookings();
        return Response.success(bookings);
    }

    @GetMapping("/{id}")
    public Response<Booking> getBookingById(@PathVariable Integer id) {
        Booking booking = bookingService.getBookingById(id);
        if (booking != null) {
            return Response.success(booking);
        } else {
            return Response.fail(404, "未找到对应预订信息");
        }
    }

    @GetMapping("/customer")
    public Response<List<Booking>> getBookingsByCustomer() {
        List<Booking> bookings = bookingService.getBookingsByCustomer();
        return Response.success(bookings);
    }

    @DeleteMapping("/{id}")
    public Response<Void> cancelBooking(@PathVariable Integer id) {
        try {
            bookingService.cancelBooking(id);
            return Response.success(null);
        } catch (Exception e) {
            return Response.fail(500, "取消预订失败: " + e.getMessage());
        }
    }
}